// vuex集成
const _store: any = {};
// router集成
let _router: any = {};
// routerList集成
let _routerList: any = [];
// api集成
const _api: any = {};

export const store = _store;

const apiMap = import.meta.globEager("./*/api/*.ts");
const apiList: any = {};
Object.keys(apiMap).map(el => {
  const apiItem = apiMap[el].default;
  let elStr = el.split('/');
  const modularName = elStr[1];
  for (let k in apiItem) {
    apiList[`${modularName}.${k}`] = apiItem[k]
  }
});
export const api = apiList;

const router = import.meta.globEager("./*/router/*.ts");
Object.keys(router).map(el => {
  const routerItem = router[el].default;
  let elStr = el.split('/');
  const modularName = elStr[1];

  function mapRouter(routerMap: any, upper: string, father: any) {
    for (let pageName in routerMap) {
      const upperPath = `${upper || ''}.${pageName || ''}`;
      const path = `/${modularName}${routerMap[pageName].path}`;
      const routerItem = {
        ...routerMap[pageName],
        path,
        component: import(`./${modularName}/page/${pageName}/index.vue`)
      };
      routerMap[pageName].component = import(`./${modularName}/page/${pageName}/index.vue`);
      let pathSplit = `/${modularName}${upperPath}`.replace(/[.]/g, "/").split('/');
      pathSplit[pathSplit.length - 1] = routerMap[pageName].path;
        _router[`${modularName}.${pageName}`] = {
        ...routerItem,
        path: pathSplit.join('/')
      };
      if (father) {
        routerItem.path = routerMap[pageName].path;
        father.children.push(routerItem);
      } else {
        _routerList.push(routerItem);
      }
      if (routerMap[pageName] && routerMap[pageName].children) {
        const children = Object.assign([], routerMap[pageName].children);
        routerItem.children = [];
        mapRouter(children, upperPath, routerItem)
      }
    }
  }
  mapRouter(routerItem, '', '');
});

export const routersMap = _router;
export const routersList = _routerList;
console.log(_routerList);


import { getCurrentInstance } from 'vue'
export const nav = (name: string, parameter: any) => {
  const { proxy } = getCurrentInstance() as any;
  const routerItem = _router[name];
  let { path } = routerItem;
  path = path.replace(/[?]/g, '');
  for (let k in parameter) {
    path = path.replace(`:${k}`, parameter[k]);
  }
  proxy.$root.$router.push(path);
};
